<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品" prop="clothId">
        <el-select v-model="queryParams.clothId" placeholder="请选择商品">
          <el-option
            v-for="item in ClothesList"
            :key="item.clothId"
            :label="item.name"
            :value="item.clothId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="颜色" prop="colorId">
        <el-select v-model="queryParams.colorId" placeholder="请选择颜色">
          <el-option
            v-for="item in ColorList"
            :key="item.colorId"
            :label="item.name"
            :value="item.colorId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="种类|尺寸" prop="sizeId" label-width="200">
        <el-select v-model="queryParams.sizeId" placeholder="请选择种类|尺寸">
          <el-option
            v-for="item in SizeList"
            :key="item.sizeId"
            :label="item.clonthType+' | '+item.clonthSize"
            :value="item.sizeId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店" prop="shopId">
        <el-select v-model="queryParams.shopId" placeholder="请选择门店">
          <el-option
            v-for="item in ShopsList"
            :key="item.shopId"
            :label="item.name"
            :value="item.shopId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库">
          <el-option
            v-for="item in WarehouseList"
            :key="item.warehouseId"
            :label="item.name"
            :value="item.warehouseId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['stores:clothesnum:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['stores:clothesnum:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['stores:clothesnum:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['stores:clothesnum:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clothesnumList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名" align="center" prop="clothes.name" />
      <el-table-column label="颜色" align="center" prop="color.name" />
      <el-table-column label="种类" align="center" prop="size.clonthType" />
      <el-table-column label="尺寸" align="center" prop="size.clonthSize" />
      <el-table-column label="门店" align="center" prop="shop.name" />
      <el-table-column label="仓库" align="center" prop="warehouse.name" />
      <el-table-column label="库存数量" align="center" prop="nums" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['stores:clothesnum:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['stores:clothesnum:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改衣服库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品id" prop="clothId">
          <el-select v-model="form.clothId" placeholder="请选择商品">
            <el-option
              v-for="item in ClothesList"
              :key="item.clothId"
              :label="item.name+' | '+item.color.name+' | '+item.classify.name+' | '+item.size.clonthSize"
              :value="item.clothId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店" prop="shopId">
          <el-select v-model="form.shopId" placeholder="请选择门店">
            <el-option
              v-for="item in ShopsList"
              :key="item.shopId"
              :label="item.name"
              :value="item.shopId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库" prop="warehouseId">
          <el-select v-model="form.warehouseId" placeholder="请选择仓库">
            <el-option
              v-for="item in WarehouseList"
              :key="item.warehouseId"
              :label="item.name"
              :value="item.warehouseId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存数" prop="nums">
          <el-input v-model="form.nums" placeholder="请输入库存数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listClothesnum,
  getClothesnum,
  delClothesnum,
  addClothesnum,
  updateClothesnum,
  exportClothesnum
} from '@/api/stores/clothesnum'
import { listColor } from '@/api/goods/color'
import { listSize } from '@/api/goods/size'
import { listShops } from '@/api/stores/shops'
import { listWarehouse } from '@/api/stores/warehouse'
import { listClothes } from '@/api/goods/clothes'

export default {
  name: 'Clothesnum',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 衣服库存表格数据
      clothesnumList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clothId: null,
        colorId: null,
        sizeId: null,
        shopId: null,
        warehouseId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clothId: [{ required: true, message: '商品不能为空', trigger: 'blur' }],
        colorId: [{ required: true, message: '颜色不能为空', trigger: 'blur' }],
        sizeId: [{ required: true, message: '尺寸不能为空', trigger: 'blur' }],
        shopId: [{ required: true, message: '店面不能为空', trigger: 'blur' }],
        warehouseId: [
          { required: true, message: '仓库不能为空', trigger: 'blur' }
        ],
        nums: [{ required: true, message: '库存数不能为空', trigger: 'blur' }]
      },
      // 颜色列表
      ColorList: [],
      // 尺寸列表
      SizeList: [],
      // 商店列表
      ShopsList: [],
      // 仓库列表
      WarehouseList: [],
      // 商品列表
      ClothesList: []
    }
  },
  created() {
    this.getList()
    Promise.all([
      listColor(),
      listSize(),
      listShops(),
      listWarehouse(),
      listClothes()
    ])
      .then((res) => {
        this.ColorList = res[0].rows
        this.SizeList = res[1].rows
        this.ShopsList = res[2].rows
        this.WarehouseList = res[3].rows
        this.ClothesList = res[4].rows
      })
      .catch((err) => {
        throw err
      })
  },
  methods: {
    /** 查询衣服库存列表 */
    getList() {
      this.loading = true
      listClothesnum(this.queryParams).then((response) => {
        this.clothesnumList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        clothId: null,
        colorId: null,
        sizeId: null,
        shopId: null,
        warehouseId: null,
        nums: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加衣服库存'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getClothesnum(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改衣服库存'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.ClothesList.forEach((element) => {
        if (element.clothId === this.form.clothId) {
          this.form.colorId = element.colorId
          this.form.sizeId = element.sizeId
        }
      })
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateClothesnum(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addClothesnum(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm(
        '是否确认删除衣服库存编号为"' + ids + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delClothesnum(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有衣服库存数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return exportClothesnum(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
    }
  }
}
</script>
